package com.zte.claa.inficombo.csa.app.service.custparam.impl;

//坐标转换器
public class CoordinateConverter {
		//地球半径
		private final static Double EARTH_RADIUS = 6378.137;
						
		//在中国范围内,经度相差0.0001,误差10m
		//private final static Double SCALE = 0.0001;
		//private final static Double LON_OFFSET = 10.0;
		//在中国范围内,纬度相差0.0001,误差11m
		//private final static Double LAT_OFFSET = 11.0;
		//标定的经度
		private Double lon_set;
		//标定的纬度
		private Double lat_set;
		//标定的x轴像素坐标
		private Double x_set;
		//标定的y轴像素坐标
		private Double y_set;
		//每米对应像素数据
		private Float pixel_set;
		//旋转的弧度
		private Double rotation;
		
		private Integer xFlag = 1;  //x轴符号
		private Integer yFlag = 1;	//y轴符号
		
		private Integer zoneid = 0; //区域id
		
		//获取角度
		private static Double rad(double d)
	    {
	        return d * Math.PI / 180.0;
	    }
		  
		 //获取gps坐标之间距离,单位是m
	    private static Double GetDistance(double lat1, double lng1, double lat2, double lng2)
	    {
	        double radLat1 = rad(lat1);
	        double radLat2 = rad(lat2);
	        double a = radLat1 - radLat2;
	        double b = rad(lng1) - rad(lng2);
	        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + 
	        Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));	       
	        s = Math.round(s * EARTH_RADIUS * 1000); //km->m,这里返回整数
	        return s;
	    }
	    
	    	    
	    //转换为x轴像素坐标(未旋转)
		public Double getpixelX(Double lon){	
			if(lon < lon_set) {
				xFlag = -1;
			}else{
				xFlag = 1;
			}
			//Long ret = Math.round(GetDistance(lat_set, lon_set, lat_set, lon) * pixel_set.doubleValue()) * xFlag + x_set.longValue();	
			Double ret = GetDistance(lat_set, lon_set, lat_set, lon) * pixel_set.doubleValue() * xFlag + x_set;	
			if(ret < 0.0) 
				ret = 0.0;	
			
			return ret;
		} 
	  		
  		//转换为y轴像素坐标(未旋转)	
  		public Double getpixelY(Double lat){
  			if(lat > lat_set) {
  				yFlag = -1;
  			} 				
  			else{
  				yFlag = 1;
  			} 			
  			//Long ret = Math.round(GetDistance(lat_set, lon_set, lat, lon_set) * pixel_set.doubleValue()) * yFlag + y_set.longValue();
  			Double ret = GetDistance(lat_set, lon_set, lat, lon_set) * pixel_set.doubleValue() * yFlag + y_set;
  			if(ret < 0.0) 
				ret = 0.0;
  			
			return ret;
  		}
  		
  		
  	
  		public void converterPoint(CoordinateInfo point){			
  			if (point == null) 
  				return;
 					
			Double xTemp = getpixelX(point.getLon());
  			Double yTemp = getpixelY(point.getLat());
  			
  	  		try{	
  	  			Long x = Math.round((xTemp - x_set) * Math.cos(rotation) - (yTemp - y_set) * Math.sin(rotation) + x_set);			
  	  			Long y = Math.round((yTemp - y_set) * Math.cos(rotation) + (xTemp - x_set) * Math.sin(rotation) + y_set);	
  	  			
  	  			point.setxLong(x);
  	  			point.setyLong(y);
  			}catch(Exception e){ 				
  				point.setxLong(Math.round(xTemp));
  	  			point.setyLong(Math.round(yTemp));
  			}
			 			
  		}

		
  		
		public Double getLon_set() {
			return lon_set;
		}
		public void setLon_set(Double lon_set) {
			this.lon_set = lon_set;
		}
		public Double getLat_set() {
			return lat_set;
		}
		public void setLat_set(Double lat_set) {
			this.lat_set = lat_set;
		}
		public Double getX_set() {
			return x_set;
		}
		public void setX_set(Double x_set) {
			this.x_set = x_set;
		}
		public Double getY_set() {
			return y_set;
		}
		public void setY_set(Double y_set) {
			this.y_set = y_set;
		}
		public Float getPixel_set() {
			return pixel_set;
		}
		public void setPixel_set(Float pixel_set) {
			this.pixel_set = pixel_set;
		}

		public Integer getxFlag() {
			return xFlag;
		}

		public void setxFlag(Integer xFlag) {
			this.xFlag = xFlag;
		}

		public Integer getyFlag() {
			return yFlag;
		}

		public void setyFlag(Integer yFlag) {
			this.yFlag = yFlag;
		}

		public Integer getZoneid() {
			return zoneid;
		}

		public void setZoneid(Integer zoneid) {
			this.zoneid = zoneid;
		}

		@Override
		public String toString() {
			return "CoordinateConverter [lon_set=" + lon_set + ", lat_set=" + lat_set + ", x_set=" + x_set + ", y_set="
					+ y_set + ", pixel_set=" + pixel_set + ", rotation=" + rotation + ", xFlag=" + xFlag + ", yFlag="
					+ yFlag + ", zoneid=" + zoneid + "]";
		}

		public Double getRotation() {
			return rotation;
		}

		public void setRotation(Double rotation) {
			this.rotation = rotation;
		}
				
}
